/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.projetjava.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.*;
import fr.unice.miage.projetjava.entities.LigneDePanier;
import java.util.Collection;

/**
 *
 * @author Olivier
 */
public class MenuPanier extends Composite {
    private VerticalPanel panelPanier = new VerticalPanel();
    private HTML panierLink = new HTML("<a href='javascript:void(0)'>Voir le panier</a>");
    private HTML validerPanierLink = new HTML("<a href='javascript:void(0)'>Comfirmer la commande</a>");
    private HTML viderPanierLink = new HTML("<a href='javascript:void(0)'>Vider le panier</a>");
    private Label messageServeur = new Label();
    private FlexTable table = new FlexTable();
    private Collection<LigneDePanier> panier = null;
    
    
    public MenuPanier()
    {
        //tablePanier();
        panelPanier.add(messageServeur);
        panelPanier.add(table);
        panelPanier.add(panierLink);
        panelPanier.add(viderPanierLink);

        final AsyncCallback callbackPanier = new AsyncCallback() {

            public void onSuccess(Object result) {
                Collection<LigneDePanier> listPanier = (Collection<LigneDePanier>)result;
                if(listPanier.equals(null))
                {
                    Popup p = new Popup("Votre panier est vide...", "auto");
                    int left = panelPanier.getAbsoluteLeft() - 50;
                    int top = panelPanier.getAbsoluteTop() + 10;
                    p.setPopupPosition(left, top);
                    p.show();
                }
                else
                {
                    DialogBox dlg = new Dialog(listPanier);
                    int left = panierLink.getAbsoluteLeft() - 500;
                    int top = panierLink.getAbsoluteTop() + 10;
                    dlg.setPopupPosition(left, top);
                    dlg.show();
                }
            }

            public void onFailure(Throwable caught) {
            }
        };

        final AsyncCallback callbackValidate = new AsyncCallback() {

            public void onSuccess(Object result) {
            }

            public void onFailure(Throwable caught) {
            }
        };

        final AsyncCallback callbackDelete = new AsyncCallback() {

            public void onSuccess(Object result) {
            }

            public void onFailure(Throwable caught) {
            }
        };

        panierLink.addClickListener(new ClickListener() {

            public void onClick(Widget w) {
                // Make remote call. Control flow will continue immediately and later
                // 'callback' will be invoked when the RPC completes.
                getService().voirPanier(callbackPanier);
            }
        });

        viderPanierLink.addClickListener(new ClickListener() {

            public void onClick(Widget w) {
                // Make remote call. Control flow will continue immediately and later
                // 'callback' will be invoked when the RPC completes.
                //getService().connexionUser(login.getText(), pwd.getText(), callbackDelete);
            }
        });

        panelPanier.addStyleName(".gwt-DialogBox");
        initWidget(panelPanier);
    }

    private void tablePanier() {

        final AsyncCallback callbackUpdatePanier = new AsyncCallback() {

            public void onSuccess(Object result) {
                panier = (Collection<LigneDePanier>) result;
                if (panier == null) {
                    messageServeur.setText("Votre panier est vide");
                } else {
                    int i = 0;
                    for (LigneDePanier ldp : panier) {
                        table.setHTML(i, 0, ldp.getProd().getProduct_name());
                        table.setHTML(i, 1, String.valueOf(ldp.getProd().getProduct_price()));
                        i++;
                    }
                }
            }

            public void onFailure(Throwable caught) {
                messageServeur.setText("Erreur lors de la mise à jour du panier");
            }
        };

        getService().updatePanier(callbackUpdatePanier);
    }

    public static MenuClientTestAsync getService(){
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of
        // the interface. The cast is always safe because the generated proxy
        // implements the asynchronous interface automatically.
        MenuClientTestAsync service = (MenuClientTestAsync) GWT.create(MenuClientTest.class);
        // Specify the URL at which our service implementation is running.
        // Note that the target URL must reside on the same domain and port from
        // which the host page was served.
        //
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        String moduleRelativeURL = GWT.getModuleBaseURL() + "menuclienttest";
        endpoint.setServiceEntryPoint(moduleRelativeURL);
        return service;
    }
}
